package cn.tedu.csmallpassport.service.impl;

import cn.tedu.csmallpassport.ex.ServiceException;
import cn.tedu.csmallpassport.mapper.AdminMapper;
import cn.tedu.csmallpassport.pojo.dto.AdminAddNewDTO;
import cn.tedu.csmallpassport.pojo.entity.Admin;
import cn.tedu.csmallpassport.service.AdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *处理管理员业务接口的实现类
 */
@Service
public class AdminImpl implements AdminService {
    @Autowired
    AdminMapper mapper;
    @Override
    public void AddNew(AdminAddNewDTO adminAddNewDTO) {
        //判断用户名、手机号、邮箱是否已存在
        String name = adminAddNewDTO.getUsername();
        String phone = adminAddNewDTO.getPhone();
        String email = adminAddNewDTO.getEmail();
        int n = mapper.countByName(name);
        if (n>0){
            String message = "添加失败，该用户名已存在";
            throw new ServiceException(message);
        }
        int p = mapper.countByPhone(phone);
        if (p>0){
            String message = "添加失败，该手机号已存在";
            throw new ServiceException(message);
        }
        int e = mapper.countByEmail(email);
        if (e>0){
            String message = "添加失败，该邮箱已存在";
            throw new ServiceException(message);
        }
        //创建管理员对象
        Admin admin = new Admin();
        BeanUtils.copyProperties(adminAddNewDTO,admin);
        mapper.insert(admin);
    }
}
